# -*- coding: utf-8 -*-
#xml 转 txt

import xml.etree.ElementTree as ET
import os,cv2
from os import listdir
from os.path import join

classes = ["person","dog","2"]  # 自己数据集有哪些类别写哪些类，按照顺序



def convert(size, box):
    dw = 1. / (size[0])
    dh = 1. / (size[1])
    x = (box[0] + box[1]) / 2.0 - 1
    y = (box[2] + box[3]) / 2.0 - 1
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x * dw
    w = w * dw
    y = y * dh
    h = h * dh
    return (x, y, w, h)


def convert_annotation(image_id):
    in_file = open('/mnt/data/song/car/labels_xml/%s.xml' % (image_id), encoding='utf-8')
    out_file = open('/mnt/data/song/car/labels_txt/%s.txt' % (image_id), 'w')
    tree = ET.parse(in_file)
    root = tree.getroot()
    size = root.find('size')
    w = int(size.find('width').text)
    h = int(size.find('height').text)
    if(w==0 or h == 0):
        imgName = in_file.buffer.name.replace("Annotations","JPEGImages").replace("xml","jpg")
        img = cv2.imread(imgName)
        _,w,h = img.shape[::-1]


    for obj in root.iter('object'):
        difficult = obj.find('difficult').text
        cls = obj.find('name').text
        if cls not in classes or int(difficult) == 1:
            continue
        cls_id = classes.index(cls)
        xmlbox = obj.find('bndbox')
        b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
             float(xmlbox.find('ymax').text))
        try:
            bb = convert((w, h), b)
        except:
            print("error")
        out_file.write(str(3) + " " + " ".join([str(a) for a in bb]) + '\n')
    out_file.close()

val_percent = 0.1  # 测试集占总数据集的比例，默认0.1，如果测试集和训练集已经划分开了，则修改相应代码
data_path = '/mnt/data/song/car/100/'  # 在darknet文件夹的相对路径，见github的说明，根据自己需要修改，注意此处也可以用绝对路径
if not os.path.exists('/mnt/data/song/car/labels_txt/'):
    os.makedirs('labels/')
image_ids = [f for f in os.listdir('/mnt/data/song/car/labels_xml')]  # 存放XML数据的文件夹
# train_file = open('/mnt/data/Pyproject/head/head_data/SCUT_HEAD_Part_B/train.txt', 'w')
# val_file = open('/mnt/data/Pyproject/head/head_data/SCUT_HEAD_Part_B/val.txt', 'w')
for i, image_id in enumerate(image_ids):
    # if image_id[-3:] == "xml":  # 有些时候jpg和xml文件是放在同一文件夹下的，所以要判断一下后缀
        # if i < (len(image_ids) * val_percent):
            # val_file.write(data_path + '%s\n' % (image_id[:-3] + 'jpg'))
        # else:
        #     train_file.write(data_path + '%s\n' % (image_id[:-3] + 'jpg'))
    convert_annotation(image_id[:-4])
# train_file.close()
# val_file.close()